package com.dmbjz.coredemo.service;


import com.dmbjz.coredemo.event.LoginSuccessEvent;
import com.dmbjz.coredemo.po.UserPo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SysService {

    public void recordLog(String userName){
        try {
            TimeUnit.SECONDS.sleep(3);
            log.info("用户：{} 记录日志信息", userName);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 监听事件方式，进行业务处理
     *
     * @param event 需要监听的事件
     */
    //通过注解监听事件，排序需要写在方法上
    @Order(3)
    @EventListener
    public void onEvent(LoginSuccessEvent event){
        UserPo source = (UserPo) event.getSource();
        log.info("SysService 感知到 LoginSuccessEvent 事件，获取到的数据：{}", source);
        recordLog(source.getUserName());
    }

}
